/* Estimates by Quartile: models with interactions */
cd "C:\Users\Public\Documents\ImmPanelRevis19\ImmPanelRevis19\Donnees\select\"

capture program drop wagesog
program define wagesog
version 11
syntax, occp(name) 

/********************/
/* Balanced Sample  */
/********************/
use final.dta, clear

drop if missing(basman)
drop if missing(bascom)
/* location in the decile of the initial group distribution */
gen lsnd=ln(sn/dp)
drop if missing(lsnd)
/* balanced the sample */
drop if (btime==1 & missing(F.lsnd))
drop if (btime==2 & missing(L.lsnd))

/* create quartiles */
egen qze = fastxtile(lsnd) if btime==1, by(ze an) nq(4)
gen q1=(qze==1 | L.qze==1)
gen q2=(qze==2 | L.qze==2)
gen q3=(qze==3 | L.qze==3)
gen q4=(qze==4 | L.qze==4)


/* keep individuals in the occupation group */
keep if (btime==1 & `occp') | (btime==2 & L.`occp')

foreach QT in q1 q2 q3 q4{
foreach OCC in bcq bcnq tecemp2 cad{
gen `QT'`OCC'=((btime==1 & `OCC') | (btime==2 & L.`OCC'))
gen dimm`QT'`OCC'=dimm*`QT'`OCC'
gen dimm75`QT'`OCC'=dimm75*`QT'`OCC'
}
}

/* residual wages for each year */
quietly: reg lsnd F_* 
predict rlsnd, residuals
/* change in residual wages */
gen drlsnd=D.rlsnd

gen ldp=ln(dp/l.dp)
gen oshift=(`occp'==0 & L.`occp'==1)
keep if btime==2 & L.`occp' & !missing(dimm) & !missing(dimm75)
foreach year in 1982 1991 1999 2007 {
/* winsorize drlsnd */
quietly: sum drlsnd if an==`year', d
replace drlsnd=. if drlsnd>`r(p99)' & an==`year'
replace drlsnd=. if drlsnd<`r(p1)' & an==`year'
}

drop if missing(drlsnd)
/* weight inverse of the size of the group in the CZ */
save temp.dta, replace
gen dummy=1
collapse (sum) nbze = dummy , by(ze an)
gen iw2=1/ nbze
gen poid=sqrt(nbze)

save iw2.dta, replace
use temp.dta, replace
capture drop _merge
joinby ze an using iw2.dta , unm(m)
drop _merge

end
capture rm "C:\Users\Public\Documents\Tab21\TableA6.rtf"

wagesog, occp(q1) 

ivreg2 drlsnd (dimm  = dimm75 ) Y_* [aweight = iw2], partial(Y_* ) cl(ze)
estimates store Q11

ivreg2 drlsnd (dimm dimmq1bcnq = dimm75 dimm75q1bcnq ) Y_* [aweight = iw2], partial(Y_* ) cl(ze)
estimates store Q12

ivreg2 drlsnd (dimm dimmq1bcq dimmq1bcnq = dimm75 dimm75q1bcq  dimm75q1bcnq ) Y_* [aweight = iw2], partial(Y_* ) cl(ze)
estimates store Q13

estout Q11 Q12 Q13 , cells(b(star fmt(%9.3f)) ///
se(par(`"="("'`")""'))) stats(N r2 widstat) starlevels(* 0.10 ** 0.05 *** 0.01)

esttab Q11 Q12 Q13  ///
using "C:\Users\Public\Documents\Tab21\TableA6.rtf" /// 
, append title("`occp'")  b(%9.3f) cells(b(star fmt(3)) se(fmt(3) par) ) ///
 stats(N widstat) star(* 0.10 ** 0.05 *** 0.01)

wagesog, occp(q2)

ivreg2 drlsnd (dimm  = dimm75 ) Y_* [aweight = iw2], partial(Y_* ) cl(ze)
estimates store Q21

ivreg2 drlsnd (dimm dimmq2bcnq = dimm75 dimm75q2bcnq ) Y_* [aweight = iw2], partial(Y_* ) cl(ze)
estimates store Q22

ivreg2 drlsnd (dimm dimmq2bcq dimmq2bcnq = dimm75 dimm75q2bcq  dimm75q2bcnq ) Y_* [aweight = iw2], partial(Y_* ) cl(ze)
estimates store Q23

estout Q21 Q22 Q23 , cells(b(star fmt(%9.3f)) ///
se(par(`"="("'`")""'))) stats(N r2 rkf) starlevels(* 0.10 ** 0.05 *** 0.01)

esttab Q21 Q22 Q23 ///
using "C:\Users\Public\Documents\Tab21\TableA6.rtf" /// 
, append title("`occp'")  b(%9.3f) cells(b(star fmt(3)) se(fmt(3) par) ) ///
 stats(N widstat) star(* 0.10 ** 0.05 *** 0.01)
